Advanced ASP.NET Core 3 Security by Scott Norberg

Advanced ASP.NET Core 3 Security by Scott Norberg

Author:Scott Norberg
Language: eng
Format: epub
ISBN: 9781484260142
Publisher: Apress


byte[] fileBytes;

var asFile = (IFormFile)value;

using (var stream = asFile.OpenReadStream())

{

fileBytes = new byte[stream.Length];

for (int i = 0; i < stream.Length; i++)

{

fileBytes[i] = (byte)stream.ReadByte();

}

}

var ext = System.IO.Path.GetExtension(asFile.FileName);

switch (ext)

{

case ".jpg":

case ".jpeg":

//If the first three bytes don't match the expected,

//fail the check

if (fileBytes[0] != 255 ||

fileBytes[1] != 216 ||

fileBytes[2] != 255)

return new ValidationResult("Image appears not " +

"to be in jpg format. Please try another.");

//If the fourth byte doesn't match one of the four

//expected values, fail the check

else if (fileBytes[3] != 219 &&

fileBytes[3] != 224 &&

fileBytes[3] != 238 &&

fileBytes[3] != 225)

return new ValidationResult("Image appears not " +

"to be in jpg format. Please try another.");

else

//All expected bytes match

return ValidationResult.Success;



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.